package pers.sen.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.sen.health.entity.PageResult;
import pers.sen.health.entity.QueryPageBean;
import pers.sen.health.mapper.CheckItemMapper;
import pers.sen.health.po.CheckItem;
import pers.sen.health.service.CheckItemService;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/11/5 9:07
 * @Description TODO
 */
@Service
@Transactional
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService {

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        /**
         * mp的Page类提供了分页查询的方法page，通过BaseMapper中selectPage实现
         *
         * page = page(new Page<T>(CurrentPage,PageSize),queryWrapper)
         * page：总条数getTotal()，查询结果getRecords()
         * CurrentPage：查询当前页
         * PageSize：页面显示条数
         * queryWrapper：查询条件
         */

        // 1. 构造条件
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);

        // 2. page方法返回查询结果
        Page<CheckItem> page = null;
        if (StringUtils.isBlank(queryPageBean.getQueryString())) {
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        } else {
            queryWrapper.like("name", queryPageBean.getQueryString());
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        }

        // 3. 封装结果集
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public Boolean updateIsDeleteById(CheckItem checkitem) {
        //  逻辑删除
        checkitem.setIs_delete(1);
        checkitem.setName(null);
        return updateById(checkitem);
    }

}
